<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * @property user_model $user_model
 * @property CI_DB_active_record $db
 * @property CI_DB_forge $dbforge
 * @property CI_Benchmark $benchmark
 * @property CI_Calendar $calendar
 * @property CI_Cart $cart
 * @property CI_Config $config
 * @property CI_Controller $controller
 * @property CI_Email $email
 * @property CI_Encrypt $encrypt
 * @property CI_Exceptions $exceptions
 * @property CI_Form_validation $form_validation
 * @property CI_Ftp $ftp
 * @property CI_Hooks $hooks
 * @property CI_Image_lib $image_lib
 * @property CI_Input $input
 * @property CI_Lang $lang
 * @property CI_Loader $load
 * @property CI_Log $log
 * @property CI_Model $model
 * @property CI_Output $output
 * @property CI_Pagination $pagination
 * @property CI_Parser $parser
 * @property CI_Profiler $profiler
 * @property CI_Router $router
 * @property CI_Session $session
 * @property CI_Sha1 $sha1
 * @property CI_Table $table
 * @property CI_Typography $typography
 * @property CI_Unit_test $unit_test
 * @property CI_Upload $upload
 * @property CI_URI $uri
 * @property CI_User_agent $user_agent
 * @property CI_Zip $zip
 * @property CI_Javascript $javascript
 * @property CI_Jquery $jquery
 * @property CI_Utf8 $utf8
 * @property CI_Security $security
 * @property file_upload_model $file_upload_model
 * @property email_model $email_model
 * @property table_model $table_model
 * @property products_model $products_model
 * @property order_model $order_model
 * @property store_model $store_model
 * @property order_detail_model $order_detail_model
 * @property information_model $information_model
 */

class MY_Controller extends CI_Controller
{
    public $current_authority;

    public function __construct()
    {
        parent::__construct();
		$this->load->database();
        $this->detect_language();
//        $this->initialize_permission();
    }

    public function detect_language()
    {
        $uri = $this->uri->uri_string();
        $segment = explode('/',$uri);
        $segment = $segment[0];
        if( $segment == 'event')
        {
            $this->session->set_userdata('lang','en');
            $this->lang->load('main','english');
        }
        else
        {
            $this->session->set_userdata('lang','vi');
            $this->lang->load('main','vietnamese');
        }
    }


    public function get_current_language_code()
    {
        return $this->session->userdata('lang');
    }


    function array_to_object($array)
    {
        if(!is_array($array))
        {
            return $array;
        }

        $object = new stdClass();
        if (is_array($array) && count($array) > 0)
        {
            foreach ($array as $name=>$value) {
                $name = strtolower(trim($name));
                if (!empty($name)) {
                    $object->$name = $this->array_to_object($value);
                }
            }
            return $object;
        }
        else
        {
            return FALSE;
        }
    }

    public function send_mail($from,$to,$name,$subject,$message,$file_attach='')
    {
        $config[]= array('protocol'=>'smtp',
            'smtp_host'=>'localhost',
            'smtp_user'=>'',
            'smtp_pass'=>'',
            'smtp_port'=>'',
            'mailtype' =>'html',
            'charset' => 'utf-8');

        $this->email->initialize($config);

        $this->email->set_newline("\r\n");

        //From email and sender name
        $this->email->from($from,$name);

        //To addresses. if more than once, add it as comma separated
        $this->email->to($to);
        $this->email->subject($subject);
        $this->email->message($message);
        if($file_attach != '')
        {
            $this->email->attach($file_attach);
            $file_attach ='';
        }
        $this->email->send();
    }


    public function rely_mail($from,$to,$name,$subject,$message)
    {
        $config[]= array('protocol'=>'smtp',
            'smtp_host'=>'localhost',
            'smtp_user'=>'',
            'smtp_pass'=>'',
            'smtp_port'=>'',
            'mailtype' =>'html',
            'charset' => 'utf-8');

        $this->email->initialize($config);

        $this->email->set_newline("\r\n");
        $this->email->clear(TRUE);

        //From email and sender name
        $this->email->from($from,$name);
        //To addresses. if more than once, add it as comma separated
        $this->email->to($to);
        $this->email->subject($subject);
        $this->email->message($message);
        $this->email->send();
    }

	public function resize_image($image_path,$new_file_path,$width,$height,$is_maintain_ratio = false)
	{
		//$file_info = get_file_info($image_path);
		//if($file_info['size'] > 4 * 1024 * 1024) return false;

		$config['image_library'] = 'gd2';
		$config['source_image']	= $image_path;
		$config['new_image']	= $new_file_path;
		$config['create_thumb'] = TRUE;
		$config['thumb_marker'] = '';
		$config['maintain_ratio'] = $is_maintain_ratio;
		$config['width']	 = $width;
		$config['height']	= $height;
		$this->image_lib->initialize($config);
        if(!$this->image_lib->resize())
            echo $this->image_lib->display_errors();
	}

    public function current_user_can($permission)
    {

//        return $this->permission_view_model->can($this->current_authority,$permission);
    }

    public function initialize_permission()
    {
        $this->permission_view_model->add_authority('SUPER_ADMIN');

    }

}